const path = require('path')

const { fileUploadError, unSupportedFileType, publishGoodsError, invalidGoodsID } = require("../constant/err.type")

const jwt = require('jsonwebtoken');

const { createGoods, updateGoods, removeGoods, restoreGoods, findGoods } = require('../service/goods.service')


class GoodsController {
    async upload(ctx, next) {
        const { file } = ctx.request.files
        // 文件格式校验
        const fileTypes = ['image/jpeg', 'image/png']
        if (file) {
            if (!fileTypes.includes(file.mimetype)) {
                return ctx.app.emit('error', unSupportedFileType, ctx)
            }
            ctx.body = {
                code: 0,
                message: "图片上传成功",
                // 文件名称
                result: {
                    goods_img: path.basename(file.filepath)
                }
            }
        } else {
            return ctx.app.emit('error', fileUploadError, ctx)
        }

    }
    // 发布商品
    async create(ctx) {
        try {
            const { createdAt, updatedAt, ...res } = await createGoods(ctx.request.body)
            ctx.body = {
                code: 0,
                message: '发布商品成功',
                result: res
            }
        } catch (error) {
            console.error(error)
            return ctx.app.emit('error', publishGoodsError, ctx)
        }
    }
    //修改商品
    async update(ctx) {
        try {
            const res = await updateGoods(ctx.params.id, ctx.request.body)
            if (res) {
                ctx.body = {
                    code: 0,
                    message: "修改商品成功",
                    result: ""
                }
            } else {
                return ctx.app.emit("error", invalidGoodsID, ctx)
            }
        } catch (error) {
            console.error(error)
        }
    }
    // 删除商品
    async remove(ctx) {
        try {
            const res = await removeGoods(ctx.params.id)
            if (res) {
                ctx.body = {
                    code: 0,
                    message: "下架商品成功",
                    result: ""
                }
            } else {
                return ctx.app.emit("error", invalidGoodsID, ctx)
            }
        } catch (error) {
            console.error(error)
        }
    }
    // 上架商品
    async restore(ctx) {
        try {
            const res = await restoreGoods(ctx.params.id)
            if (res) {
                ctx.body = {
                    code: 0,
                    message: "上架架商品成功",
                    result: ""
                }
            } else {
                return ctx.app.emit("error", invalidGoodsID, ctx)
            }
        } catch (error) {
            console.error(error)
        }
    }
    async findAll(ctx) {
        // 解析pageSize pageNum
        const { pageNum = 1,pageSize = 10 } = ctx.request.query
        // 调用数据处理相关方法
        const res = await findGoods(pageNum, pageSize)

        // 返回数据结果
        ctx.body = {
            code: 0,
            message: "获取商品列表成功",
            result: res
        }
    }
}
module.exports = new GoodsController()